#include <lock.h>

#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>

int LockAFile(char* filename){
	char lockfile[128];
	strcpy(lockfile, filename); 
	int fd=open(strcat(lockfile, ".lck"), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
	if(fd<0){
		return 0;
	}else{
		close(fd);
		return 1;
	}
}

int IsFileUnlocked(char* filename){
	char lockfile[128];
	strcpy(lockfile, filename); 

	int fd;
	if((fd=open(strcat(lockfile, ".lck"), O_RDONLY))>=0){
		close(fd);
        return 0;
    }
    return 1;
}

void UnlockAFile(char* filename){
	char lockfile[128];
	strcpy(lockfile, filename);

	unlink(strcat(lockfile, ".lck"));
}

int FileExists(char* filename){
	int fd;
	if((fd=open(filename, O_RDONLY))>=0){
		close(fd);
        return 1;
    }
    return 0;
}
